package leetcode.N1_N100;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/19 5:24 下午
 */
public class N71 {


    public String simplifyPath(String path) {
        String[] split = path.split("/");
        String[] stack = new String[split.length];
        int index = 0;
        String empty = "";
        String current = ".";
        String last = "..";
        for (String s : split) {
            if (empty.equals(s) || current.equals(s)) {
                continue;
            } else if (last.equals(s)) {
                if (index > 0) {
                    index--;
                }
            } else {
                stack[index++] = s;
            }
        }
        if (index == 0) {
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < index; i++) {
            sb.append("/").append(stack[i]);
        }
        return sb.toString();
    }

}
